package com.ljl.basic.interceptor;


import com.ljl.basic.annotation.PreAuthorize;
import com.ljl.basic.jwt.JwtUtils;
import com.ljl.basic.jwt.LoginData;
import com.ljl.basic.jwt.Payload;
import com.ljl.basic.jwt.RsaUtils;
import com.ljl.user.domain.Logininfo;
import com.ljl.user.mapper.LogininfoMapper;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PublicKey;
import java.util.List;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (token != null) {
            try {
                // 获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(RsaUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                // 解密
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);

                if (payload != null) {
                    Logininfo logininfo = payload.getLoginData().getLogininfo();
                    if (logininfo.getType() == 1) {
                        return true;
                    }

                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    PreAuthorize preAuthorize = handlerMethod.getMethodAnnotation(PreAuthorize.class);
                    if (preAuthorize == null) {
                        return true;
                    } else {
                        List<String> sns = logininfoMapper.findPermissionByLogininfoId(logininfo.getId());
                        String sn = preAuthorize.sn();
                        if (sns.contains(sn)) {
                            return true;
                        } else {
                            response.setContentType("application/json; charset=UTF-8");
                            response.getWriter().println("{\"success\":false,\"message\":\"noPermission\"}");
                            return false;
                        }
                    }
                    // return true;
                }

            } catch (ExpiredJwtException e) {
                response.setContentType("application/json; charset=UTF-8");
                response.getWriter().println("{\"success\":false,\"message\":\"timeout\"}");
                return false;
            }
        }
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().println("{\"success\":false,\"message\":\"noLogin\"}");
        return false;
    }
}
